package com.uzai.console.aop;

import com.alibaba.fastjson.JSONObject;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.annotation.NoRepeatSubmit;
import com.uzai.console.annotation.UserAccDetailInsert;
import com.uzai.console.common.UzaiRespVo;
import com.uzai.console.dto.feign.uzaimanage.SysRunlogWarningDto;
import com.uzai.console.dto.user.useraccdetail.UserBizLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * @功能描述 aop解析注解
 * @author liuqi
 * @date 2023年01月10日 13:39
 */
@Aspect
@Component
public class UserAccDetailInsetAop {
    private static Logger logger = LoggerFactory.getLogger(UserAccDetailInsetAop.class);

    @Autowired
    private RedisTemplate redisTemplate;

    @Around("execution(* com.uzai.console.mapper.UserAccDetailMapper.insert(..)) && @annotation(nrs)")
    public Object arround(ProceedingJoinPoint joinPoint, UserAccDetailInsert nrs) throws Throwable{

        Object result = null;
        Object target = joinPoint.getTarget();
        String methodName = joinPoint.getSignature().getName();
        StopWatch stopWatch = new StopWatch();
        //所有参数值
        Object[] args  = joinPoint.getArgs();
        try {
            //进入方法之前，执行操作-相当于前置

            //进入方法
            result = joinPoint.proceed();

            //进入方法之后执行-相当于后置
            Logger bizlogger = LoggerFactory.getLogger("user_info_log");
            if(args != null && args.length > 0){
                bizlogger.info(JSONObject.toJSONString(new UserBizLog(1,args[0])));
            }
        }catch (Throwable e){////异常增强
            if(args != null && args.length > 0){
                logger.error("--会员账号明细异常--"+"args[]=" + JSONObject.toJSONString(args[0]) + "\n######"+ target.getClass().getName()+"：Exception of the method "+methodName+": "+e.getMessage());
            }else{
                logger.error("--会员账号明细异常--" + target.getClass().getName()+"：Exception of the method "+methodName+": "+e.getMessage());
            }
            throw e;
        }

        return result;
    }
}
